package lol_stream_manager;

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class characterFinder {
    
    public static void characterFinder( String channel ) throws Exception {
        RTMP_Dumper test = new RTMP_Dumper( channel );
        characterFinderAll(channel, test);
    }

    public static void characterFinderOwned( String channel, String id ) throws Exception {
        RTMP_Dumper test = new RTMP_Dumper( channel, id );
        characterFinderAll(channel, test);
    }
    
    public static void characterFinderAll( String channel, RTMP_Dumper test) throws Exception {
        if( !test.isOnline ){
            System.out.println(channel + " offline.");
            LOL_Stream_Manager.results[0].add(new String[] {channel});
        } else if( test.flvSize == 0 ){
            System.out.println(channel + " -> impossible to get stream");
            LOL_Stream_Manager.results[3].add(new String[] {channel, " impossible to get stream."});
        }
        else {
            Utility.chronometer ch = new Utility.chronometer();
            for(int i=0 ; i<2 ; i++){
                String ss = (i == 0) ? "-ss 1 " :  "";
                File f = new File(channel + ".jpg");
                if( f.exists() )
                    f.delete();
                
                System.out.println(channel);

                execcmd.ExecCMD.exec( "C:\\Users\\Mystic\\Documents\\ffmpeg\\bin\\ffmpeg -i " + channel + ".flv "
                        + "-vf select=\"eq(pict_type\\,I)\" "
                        + ss
                        + channel + ".jpg",
                        new File(channel + "_ffmpeg.txt"));
                

//                if( f.exists() ){
//                    BufferedImage image = ImageIO.read(new File(channel + ".jpg"));
////                    double brightness = ImageFinder.getBrightness( image );
////                    System.out.println( channel + " stream keyframe brightness: " + brightness );
//                    BufferedImage tmpimage = image.getSubimage(0, image.getHeight()-120, 119, 119);
//                    System.out.println( channel + " -> " + ImageFinder.bestImage( channel, tmpimage ) + " in " + ch);
//                    return;
//                } else {
//                    if( i==0)
//                        System.out.println( channel + " -> " + "impossible to get the 2nd keyframe.");
//                    else {
//                        System.out.println( channel + " -> " + "impossible to get the 1st keyframe.");
//                        LOL_Stream_Manager.results[3].add(new String[] {channel, " no keyframe in stream."});
//                    }
//                }
            }
        }
    }
    
}
